//
//  STChannelCardCell.swift
//  StarTimesON
//
//  Created by Toj on 9/26/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class STTVCellModel: NSObject {
    var url = ""
    var name = ""
    var tag = ""
    var t_bcolor: UIColor = .random
    var t_tcolor: UIColor = .random
}

class STTVCell: UICollectionViewCell {
    
    var cellModel: STTVCellModel? { didSet {
        guard let model = cellModel else { return }
         
        tagLabel.text = model.tag
        tagLabel.backgroundColor = model.t_bcolor
        tagLabel.textColor = model.t_tcolor
        nameLabel.text = model.name
        
        let url = URL(string: model.url)
        imageView.kf.setImage(with: url, placeholder: "pay_ment_default".to_image)
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupAddSubViews()
        setupLayoutSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .center
        view.clipsToBounds = true
        return view
    }()
    
    private lazy var tagLabel: UILabel = {
        let view = UIInsetLabel()
        let fs = Int(arc4random_uniform(5)) + 8
        view.font = fs.to_boldFont
        view.lbl_contentInsets = UIEdgeInsets(top: 1, left: 2, bottom: 1, right: 2)
        view.cm_corner(radius: 2, corners: .ca_layerBottomRightCorner)
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.font = 12.to_boldFont
        view.textColor = .random
        return view
    }()
}

extension STTVCell {
    
    private func setupAddSubViews() {
        contentView.addSubview(imageView)
        contentView.addSubview(tagLabel)
        contentView.addSubview(nameLabel)
    }
    
    private func setupLayoutSubViews() {
        imageView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        tagLabel.snp.makeConstraints { make in
            make.left.top.equalTo(contentView)
            make.width.lessThanOrEqualTo(contentView).inset(10)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(contentView)
        }
    }
}
